package com.java.wechat.util;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.nio.charset.Charset;
import java.util.HashMap;
import java.util.Map;

import com.github.tobato.fastdfs.domain.ThumbImageConfig;
import org.apache.commons.io.FilenameUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import com.github.tobato.fastdfs.domain.StorePath;
import com.github.tobato.fastdfs.exception.FdfsUnsupportStorePathException;
import com.github.tobato.fastdfs.service.FastFileStorageClient;

@Component
public class FastDFSClient {

	@Autowired
	private FastFileStorageClient storageClient;
	@Autowired
	private ThumbImageConfig thumbImageConfig;

	/**
	 * 上传文件
	 * @param file MultipartFile文件对象
	 * @return 文件访问地址
	 */
	public String uploadFile(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadFile(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
		
		return storePath.getPath();
	}


	/**
	 * 上传文件
	 * @param file File文件对象
	 * @return 文件访问地址
	 */
	public String uploadFile(File file) throws IOException {
		StorePath storePath = storageClient.uploadFile(new FileInputStream(file), file.length(),
				FilenameUtils.getExtension(file.getName()), null);

		return storePath.getPath();
	}

    /**
     * 上传图片
     * @param file MultipartFile文件对象
     * @return 返回图片地址和缩略图地址
     */
	public Map<String,String> uploadImageAndCrtThumbImage(MultipartFile file) throws IOException {
		StorePath storePath = storageClient.uploadImageAndCrtThumbImage(file.getInputStream(), file.getSize(),
				FilenameUtils.getExtension(file.getOriginalFilename()), null);
        // 获取缩略图路径
        String thumbImagePath = thumbImageConfig.getThumbImagePath(storePath.getPath());

        Map<String,String> map = new HashMap<>();
        map.put("path",storePath.getPath());
        map.put("thumbImagePath",thumbImagePath);

        return map;
	}

    /**
     * 上传图片
     * @param file File文件对象
     * @return 返回图片地址和缩略图地址
     */
    public Map<String,String> uploadImageAndCrtThumbImage(File file) throws IOException {
        StorePath storePath = storageClient.uploadImageAndCrtThumbImage(new FileInputStream(file), file.length(),
                FilenameUtils.getExtension(file.getName()), null);
        // 获取缩略图路径
        String thumbImagePath = thumbImageConfig.getThumbImagePath(storePath.getPath());

        Map<String,String> map = new HashMap<>();
        map.put("image",storePath.getPath());
        map.put("thumbImage",thumbImagePath);

        return map;
    }
	
	/**
	 * 将一段字符串生成一个文件上传
	 * @param content
	 *            文件内容
	 * @param fileExtension
	 * @return
	 */
	public String uploadFile(String content, String fileExtension) {
		byte[] buff = content.getBytes(Charset.forName("UTF-8"));
		ByteArrayInputStream stream = new ByteArrayInputStream(buff);
		StorePath storePath = storageClient.uploadFile(stream, buff.length, fileExtension, null);
		return storePath.getPath();
	}

	/**
	 * 删除文件
	 * @param fileUrl
	 *            文件访问地址
	 * @return
	 */
	public void deleteFile(String fileUrl) {
		if (StringUtils.isEmpty(fileUrl)) {
			return;
		}
		try {
			StorePath storePath = StorePath.praseFromUrl(fileUrl);
			storageClient.deleteFile(storePath.getGroup(), storePath.getPath());
		} catch (FdfsUnsupportStorePathException e) {
			e.getMessage();
		}
	}
}
